package com.jiuzhi.app.common.wxutil;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiuzhi.app.common.util.HttpResult;
import com.jiuzhi.app.common.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


public class GetTokenUtil {
    public static String appId = "wx9e6031cb12e760e3";
    public static String secret = "b34e16f12fc259488243a4487d81c0a7";
    public static String wxurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    // 获取jsapi_ticket签名
    public static final String ticketURL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?";
    public static String token = "abcabc";
    public static String mchId = "1511829211";
    //用户授权地址;
    public static final String sitePath = "http://www.9zhinet.com";
    //获取微信公众号支付支付信息
    public static final String PAY_INFO = "CHARGE:PAY:INFO:S:KEY[%s]";
    //微信支付秘钥
    public static String paysecret = "fdjnskfds2sd2f2sfsdf212sdf2dfsbb";
    //调用充电桩服务地址
   // public static String chargeurl = "http://172.16.10.75:8112/cardsystemapi";


    public static  final Logger logger = LoggerFactory.getLogger(GetTokenUtil.class);



    /**
     * access_token是公众号的全局唯一接口调用凭据，公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。
     * access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时，需定时刷新，
     * 重复获取将导致上次获取的access_token失效。 获取微信访问令牌accessToken
     */
    public  static   String getAccessToken() {
        String accessToken = null;
        try {
            String httpUrl = wxurl + "&appId=" + appId + "&secret=" + secret;
            // 判断是否过期 日过过期重新获取 不过期从redis获取

                HttpResult httpResult = HttpUtil.httpGet(httpUrl);
                String result = httpResult.getResult();
                Map<String, String> map = new ObjectMapper().readValue(result, Map.class);

                logger.info("设置token："+map.get("access_token"));
                accessToken = map.get("access_token").replaceAll("\\\"","");


        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessToken;
    }



}
